package com.blb.controller;


import com.blb.entity.*;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IOrderService;
import com.blb.service.IProductiveProcessService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-15
 */
@SessionAttributes
@Controller
@RequestMapping("/order")
public class OrderController {
    @Resource
    private IOrderService orderService;

    @Resource
    private IProductiveProcessService productiveProcessService;


    @RequestMapping("{page}")
    public  String welcome(@PathVariable String page){
        System.out.println(page);
        return   "order/"+page;
    }

@RequiresPermissions("orderlist")
    @GetMapping("into")
    public String index(Model model){
        User user = ShiroUtils.getUser();
        System.out.println(user+"-=--------------");
        int count0 = orderService.count(user.getCompanyId(),null, null, "");
        int count1 = orderService.count(user.getCompanyId(),null, null, "1");
        int count2 = orderService.count(user.getCompanyId(),null, null, "2");
        int count3 = orderService.count(user.getCompanyId(),null, null, "3");
        int count4 = orderService.count(user.getCompanyId(),null, null, "4");
        model.addAttribute("count0",count0);
        model.addAttribute("count1",count1);
        model.addAttribute("count2",count2);
        model.addAttribute("count3",count3);
        model.addAttribute("count4",count4);
        return "order/detail_List";
    }

    @ResponseBody
    @PostMapping("page")
    public BlbPage page(int start, int length, String orderId, String dealTime,String orderStatus){

       User user = ShiroUtils.getUser();
        List<Map<String,Object>> allOrderList = orderService.findAllOrderList(user.getCompanyId(),start, length, orderId, dealTime,orderStatus);
            int count = orderService.count(user.getCompanyId(),orderId, dealTime,orderStatus);
            return new DataTablePage(count, count, allOrderList);



    }

    @ResponseBody
    @GetMapping("list")
    public List<Map<String, Object>> index1(Model model,String orderId,Date dealTime){

        List<Map<String, Object>> list = orderService.getAll(orderId,dealTime);



        return list;
    }

    @RequiresPermissions("ordersort")
    @GetMapping("sort")
    public String index1(Model model){


        return "order/order_sort";
    }

    @ResponseBody
    @PostMapping("page2")
    public BlbPage index2(Model model,int start, int length,String orderId){
        User user = ShiroUtils.getUser();
        List<Map<String, Object>> allSort = orderService.findAllSort(user.getCompanyId(),start,length,orderId);
        int countsort = orderService.countSort(user.getCompanyId(),orderId);
        return new DataTablePage(countsort, countsort, allSort);
    }

    @ResponseBody
    @RequestMapping("allproduct")
    public ResponseHelp allproduct(Model model){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<Product> allProduct = orderService.findAllProduct(companyId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allProduct);
        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("allproductionno")
    public ResponseHelp allproductionno(Model model){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<String> productById = orderService.findProductById(companyId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(productById);

        return responseHelp;
    }

    @ResponseBody
    @RequestMapping("allcustom")
    public ResponseHelp allcustom(Model model){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<Company> allCustom = orderService.findAllCustom(companyId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allCustom);

        return responseHelp;
    }

    @RequestMapping("addorderlist")
    public String  addorderlist(@RequestBody Order data, HttpSession session){
        System.out.println(data);
        String orderId = data.getOrderId();
        String productId = data.getProductId();
        String customId = data.getCustomId();
        Double money = data.getMoney();
        String orderType = data.getOrderType();
        Integer number = data.getNumber();
        String orderHolder = data.getOrderHolder();
        User user = ShiroUtils.getUser();

        Order order =new Order();
        order.setId(UUIDUtils.getId());
        order.setOrderId(orderId);
        order.setProductId(productId);
        order.setCustomId(customId);
        order.setMoney(money);
        order.setOrderType(orderType);
        order.setNumber(number);
        order.setOrderHolder(orderHolder);
        order.setOperator(user.getUserName());
        order.setSupplierId(user.getCompanyId());
        order.setPayType("银联卡");
        Date date = new Date();
        String dealTime= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        order.setDealTime(dealTime);
        order.setOrderStatus("1");

        productiveProcessService.insertP(orderId,1);
        orderService.save(order);
       return "order/detail_List";
    }

    /**
     *
     * @param model
     * @param orderId
     * @return
     */
    @RequestMapping("goproduce")
    public String goproduce(Model model,String orderId){
        Order order =new Order();
        order.setOrderId(orderId);
        order.setOrderStatus("2");
        order.setExplains("goproduce");
        order.setProductStatus("5");
        orderService.updateProduce(order);
        productiveProcessService.insertP(orderId,2);
        productiveProcessService.insertP(orderId,5);
        System.out.println(orderId);
         return  "order/detail_List";
    }

    @RequestMapping("gogoods")
    public String gogoods(Model model,String orderId){
        Order order = new Order();
        order.setOrderId(orderId);
        order.setOrderStatus("2");
        order.setExplains("godelivered");
        orderService.updateGoods(order);
        return "order/detail_List";
    }

    @RequestMapping("dealauditing")
    public String dealauditing(Model model,String orderId){
        System.out.println(orderId);
        User user = ShiroUtils.getUser();

        Map<String, Object> orderByOrderID = orderService.findOrderByOrderID(user.getCompanyId(),orderId);
        Map<String, Object> findstatus = orderService.findStatus(orderId, user.getCompanyId());

        model.addAttribute("orderByOrderID",orderByOrderID);
        model.addAttribute("findstatus",findstatus);
        return "order/deal_auditing";
    }

    @ResponseBody
    @RequestMapping("allproduction")
    public ResponseHelp allproduction(){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<ProductionLine> allProductionLine = orderService.findAllProductionLine(companyId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allProductionLine);
        return responseHelp;

    }

    @RequestMapping("updateallsort")
    public String  updateallsort(@RequestBody Order data){
        String orderId = data.getOrderId();

        String productLineId = data.getProductLineId();
        orderService.updateOne(orderId,productLineId);

        Order order = orderService.findNumberAndProduction(orderId, productLineId);
        Integer number = order.getNumber();

        Integer productionCapacity = order.getProductionCapacity();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date dt =new Date(data.getProductPlanTime().getTime()+(number/productionCapacity+1)*24*60*60*1000);

        String product_plan_overtime = dateFormat.format(dt);
        Order order1 =new Order();
        order1.setOrderId(orderId);
        order1.setProductPlanNo(data.getProductPlanNo());
        order1.setProductPlanTime(data.getProductPlanTime());
        order1.setProductPlanOvertime(dt);
        order1.setProductStatus("6");
        orderService.updateProductSort(order1);
        productiveProcessService.insertP(orderId,6);

        return "order/order_sort";
    }


    /**
     * 采购列表签收修改订单状态为已完成
     * @param orderId
     * xuyuanxin
     */
    @ResponseBody
    @RequestMapping("updateStatus")
    public ResponseHelp updateStatus(String orderId){
        orderService.updateStatus(orderId);
        productiveProcessService.insertP(orderId,4);
        orderService.updateVehicle(orderId);
        orderService.updateDriver(orderId);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;
    }
}

